Foreword: The author has 18 years of experience in hardware research and development, from (1) 51 single-chip microcomputer to (2) FPGA to (3) embedded ARM (ARM9 to CORTEX A9). Machine research and development to (2) smart home system to (3) radio monitoring machine to (4) tablet computer research and development to (5) intelligent control of street lamps to (5) industrial computers are involved, from (1) ordinary electronic technical engineers to (2) Deputy Chief Engineer to (3) Deputy General Manager to (4) General Manager of the Division. . . Now that I have entered the age of no confusion, I am passionate about IoT technology, so I decided to fully switch from hardware to spring cloud technology, one of the IoT technology frameworks, and share my entire learning experience with you. More opportunities to cooperate and discuss with you.
Today is: April 10, 2018 Research Topic: Transforming a Consumption from Service Discovery
1. From the above (3), we have established a high-availability registry and a service respectively. We put the produced JAR package on the C drive: cjb1-eureka-1.0.0.jar (highly available registry) and cjb1 -0.0.1-SNAPSHOT.jar (a service) run separately
1. Run the high availability registry:
2. Run the service on two ports: 8081 and 8082:
3. Enter in the webpage: http://localhost:1111/ and you will see that two services CJB1-SERVICE (running on ports 8081 and 8082 respectively) have been successfully registered to the euerka center (the picture may be a little small, you need a magnifying glass)
2. The above already has a registry and a service (service provider), but lacks a consumer (service caller), as shown below:
We changed the previous cjb1 project to a cjb2 project and transformed it as a service caller:
1. Add the dependency spring-cloud-starter-ribbon of the ribbon module to "pom.xml" in the cjb2 project. Reminder: To add a module dependency is to start with spring-cloud-starter and add the module name. Haha, at least it's been like this from the previous projects, of course, the complaints of the great gods are not excluded! Hey, to make software is to copy and paste, and paste it out for everyone to see the complete code:
<?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.example</groupId> <artifactId>cjb2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cjb1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.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.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.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.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>
2. Add "@Bean" and "@LoadBanlanced" to the main class "Cjb2Application" to enable client load balancing;
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class Cjb2Application { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Cjb2Application.class, args); } }
3、在“cjbcontroller”中创建“RestTemplate”对CJB1-SERVICE服务提供的/hello接口的调用。这点是关键点,也就是通过cjb2的服务调用者来调用服务提供者的数据接口,并显示出服务提供者的数据“Hello World”这样就达到了我们通过服务调用者调用服务提供者内容的目的;完整代码如下:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.client.RestTemplate; @RestController public class cjbcontroller { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/hello-ribbon" , method = RequestMethod.GET) public String say(){ return restTemplate.getForEntity("http://CJB1-SERVICE/hello",String.class).getBody(); } }
4、在“application.properties”中取名字“cjb1-ribbon”;注册到注册中心“http://localhost:1111/euerka”;定义启动端口:9001.完整代码如下:
spring.application.name=cjb1-ribbon eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ server.port=9001
三、运行cjb2项目,下面就是见证奇迹的时刻:
1、在网页中输入:http://localhost:1111/如下图,我晕,终于看到如下内容,表示跑起来两个服务提供者CJB1-SERVICE,其运行在两个端口上面8081和8082:另外还有一个服务调用者CJB1-RIBBON,运行在9001端口上面:
2、在网页中输入:http://localhost:9001/hello-ribbon,会出现“Hello World”的内容,表示调用服务提供者内容成功!
四、如果需要完整代码的朋友,可以加入作者QQ群:智物联的spring cloud,入群说明:spring cloud代码需求