搭建Spring Cloud Eureka 服务的注册和发现小项目

来源:互联网 发布:网站流量统计java代码 编辑:程序博客网 时间:2024/06/05 19:42

环境配置:

JDK 1.7 
Spring Cloud 版本 Camden.SR5
Maven 4.0.0
Spring Boot 版本   1.5.1.RELEASE

本人也是第一次学习Spring Cloud 希望分享自己的一点心得,跟大家分享出来一起学习进步!!!要是有错误的地方希望大家指正出来!!!

1.eureka服务器搭建(Eureka-Server)

pom.xml
<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.hc</groupId><artifactId>eureka-server</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.1.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR5</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>
application.properties
server.port=10001#强制不注册到注册服务器eureka.client.register-with-eureka=falseeureka.client.fetch-registry=false#注册中心地址eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
Java代码
package eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/** * @Description:  * @author    : hujingbo  * @date    : 2017-9-25 下午4:02:11 */@EnableEurekaServer@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
启动应用程序,访问http://localhost:10001/   就能看到如下页面

2.eureka服务提供者搭建(Eureka-Provider)

pom.xml
<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.hc</groupId><artifactId>eureka-provider</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.1.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR5</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>
application.properties
server.port=10002spring.application.name=eureka.servereureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
Java 代码 
实际服务提供者
package eureka;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @Description:  * @author    : hujingbo  * @date    : 2017-9-25 下午4:17:20 */@RestControllerpublic class ProviderController {@RequestMapping(value="/getname",method=RequestMethod.GET)public String getValue(@RequestParam("name") String name){return "你好:" + name;}}
应用启动类
package eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * @Description:  * @author    : hujingbo  * @date    : 2017-9-25 下午4:15:24 */@EnableEurekaClient@SpringBootApplicationpublic class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}
然后启动应用  访问http://localhost:10001/  就能看到如下页面


3.eureka服务消费者搭建(Eureka-Client)

pom.xml
<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.hc</groupId><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</java.version></properties><dependencies><!-- 将服务提供者依赖加载进来 --><dependency><groupId>com.hc</groupId><artifactId>eureka-provider</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</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-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR5</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>

application.properties 
server.port=10003spring.application.name=eureka.clienteureka.client.serviceUrl.defaultZone=http\://localhost\:10001/eureka/
Java 代码
实际服务消费方法
package eureka;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * @Description: * @author : hujingbo * @date : 2017-9-25 下午4:37:27 */@RestControllerpublic class TestService {@AutowiredRestTemplate client;/** * LoadBalanced 注解表明restTemplate使用LoadBalancerClient执行请求 *  * @return */@Bean@LoadBalancedpublic RestTemplate restTemplate() {RestTemplate template = new RestTemplate();SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) template.getRequestFactory();factory.setConnectTimeout(3000);factory.setReadTimeout(3000);return template;}@RequestMapping(value="/",method=RequestMethod.GET)public String helloWorld(@RequestParam("name") String name) {return client.getForObject("http://eureka.server/getname", String.class);  //服务访问地址一定要写正确!!!}}
服务启动类
package eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/** * @Description:  * @author    : hujingbo  * @date    : 2017-9-25 下午4:36:12 */@EnableEurekaClient@SpringBootApplicationpublic class ClientApplication {public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}}
启动服务  然后访问路径  http://localhost:10003/getname?name=小鱼儿


看到这说明服务已经调用成功了!!!


阅读全文
0 0
原创粉丝点击