将微服务注册到Eureka Server上
生活随笔
收集整理的這篇文章主要介紹了
将微服务注册到Eureka Server上
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-discovery-eureka</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}
server.port=8761
#eureka.instance.hostname=eureka1#spring.application.name=microservice-discovery-eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false#eureka.server.enableSelfPreservation=truesecurity.basic.enabled=true
security.user.name=admin
security.user.password=1234spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=truelogging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>microservice-simple-provider-user</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><!-- 引入web模塊 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot進行單元測試的模塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 引入druid數據源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.8</version></dependency><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=7900#server.context-path=/boot02spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=truelogging.level.com.learn=trace
#logging.file=D:/springboot.log
logging.file=springboot.log
#logging.path=/spring/log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n
#spring.resources.static-locations=classpath:/hello,classpath:/learnspring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://59.110.158.145:3306/SpringCloud?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xmleureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-simple-provider-user
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379
package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import com.learn.cloud.entity.User;
import com.learn.cloud.mapper.UserMapper;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@Autowiredprivate EurekaClient eurekaClient;@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping("/simple/{id}")public User findById(@PathVariable Long id) {return this.userMapper.getUserById(id);}@GetMapping("/eureka-instance")public String serviceUrl() {InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-SIMPLE-PROVIDER-USER", false);return instance.getHomePageUrl();}@GetMapping("/instance-info")public ServiceInstance showInfo() {ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();return localServiceInstance;}
}
package com.learn.cloud;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@MapperScan(value="com.learn.cloud.mapper")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceSimpleProviderUserApplication {public static void main(String[] args) {SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>microservice-simple-consumer-movie</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><dependencyManagement><dependencies><dependency> <!-- 進行SpringCloud依賴包的導入處理 --><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency> <!-- SpringCloud離不開SpringBoot,所以必須要配置此依賴包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=7901user.userServicePath: http://10.40.8.152:7900/simple/eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-simple-consumer-movie
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@EnableEurekaClient
@SpringBootApplication
public class MicroserviceSimpleConsumerMovieApplication {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version><packaging>pom</packaging><name>microcloud02</name><url>http://maven.apache.org</url><properties><jdk.version>1.8</jdk.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency> <!-- 進行SpringCloud依賴包的導入處理 --><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency> <!-- SpringCloud離不開SpringBoot,所以必須要配置此依賴包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> --></project>
第一個pom文件引入<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>第二步把微服務注冊到Eureka里面去,在啟動類里面加上注解,@EnableEurekaClient@MapperScan(value="com.learn.cloud.mapper")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceSimpleProviderUserApplication {public static void main(String[] args) {SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);}
}@EnableDiscoverClient,這兩個注解有什么區別呢,@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public @interface EnableEurekaClient {}只是他非常的語義化,@EnableDiscoveryClient這個可以使用到ZK和Consul,https://cloud.spring.io/spring-cloud-static/Camden.SR1/#netflix-eureka-client-startermaven生成jar,提示沒有“沒有主清單屬性”https://www.jianshu.com/p/fd5bd865785210.40.8.152:8761vim /etc/profile添加如下內容:JAVA_HOME根據實際目錄來
JAVA_HOME=/usr/local/software/jdk1.8.0_151
CLASSPATH=JAVAHOME/lib/PATH=PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATHsource /etc/profile10.40.8.152:8761localhost:876110.40.8.144:7900/eureka-instanceInstanceInfo getNextServerFromEureka(String virtualHostname, boolean secure);10.40.8.144:7900/instance-info{"host":"10.40.8.144","port":7900,"metadata":{},"uri":"http://10.40.8.144:7900","secure":false,"serviceId":"microservice-simple-provider-user"}{"host":"10.40.8.144","port":7900,"metadata":{},"uri":"http://10.40.8.144:7900","secure":false,"serviceId":"microservice-simple-provider-user"
}10.40.8.144:7900/envhttp://10.40.8.144:7900/health
?
總結
以上是生活随笔為你收集整理的将微服务注册到Eureka Server上的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu开放指定端口
- 下一篇: Ribbon-1 Ribbon的基本使用