Commit 66b8deb1 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

add 服务注册与服务发现

parent 9c6c8974
Pipeline #96 failed
......@@ -9,6 +9,7 @@
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
/**/**.iml
# Generated files
.idea/**/contentModel.xml
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="hello-consumer" />
<module name="hello-consumer-feign" />
<module name="hello-consumer-ribbon" />
<module name="service-discovery" />
<module name="hello-producer" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel target="8">
<module name="hello-consumer" target="1.5" />
<module name="hello-consumer-feign" target="1.5" />
<module name="hello-consumer-ribbon" target="1.5" />
<module name="hello-producer" target="1.5" />
<module name="service-discovery" target="1.5" />
<module name="spring-cloud-hello-world" target="1.5" />
<module name="spring-cloud-parent" target="1.5" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="date" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer/hello-consumer.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer/hello-consumer.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer-feign/hello-consumer-feign.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer-feign/hello-consumer-feign.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer-ribbon/hello-consumer-ribbon.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/hello-consumer-ribbon/hello-consumer-ribbon.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/hello-producer/hello-producer.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/hello-producer/hello-producer.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/service-discovery/service-discovery.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/service-discovery/service-discovery.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-hello-world/spring-cloud-hello-world.iml" filepath="$PROJECT_DIR$/spring-cloud-hello-world/spring-cloud-hello-world.iml" />
<module fileurl="file://$PROJECT_DIR$/spring-cloud-parent.iml" filepath="$PROJECT_DIR$/spring-cloud-parent.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -14,3 +14,17 @@ springboot与spring cloud兼容版本
| Edgware | 1.5.x |
| Dalston | 1.5.x |
### 应用端口配置
9000-9099: 服务注册中心端口
9100-9199: 服务提供方端口
9200-9299: 服务消费方端口
### 参考
1. https://windmt.com/2018/04/14/spring-cloud-1-services-governance/
......@@ -16,7 +16,7 @@
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
<spring.cloud.version>Greenwich.RELEASE</spring.cloud.version>
<java.version>1.8</java.version>
<lombok.version>1.18.8</lombok.version>
</properties>
<dependencyManagement>
......@@ -36,6 +36,12 @@
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......@@ -49,4 +55,4 @@
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
</project>
<?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">
<parent>
<artifactId>spring-cloud-hello-world</artifactId>
<groupId>com.duanledexian.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-consumer-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
package com.duanledexian.spring.cloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
@EnableFeignClients
public class SpringWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebApplication.class, args);
}
}
package com.duanledexian.spring.cloud.producer.api;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* The interface Hello api.
*
* @author Administrator
*/
@FeignClient(name = "service-producer")
public interface IHelloApi {
/**
* Say hello string.
*
* @param name the name
* @return the string
*/
@GetMapping("/api/v1/sayHello")
String sayHello(String name);
}
package com.duanledexian.spring.cloud.producer.controller;
import com.duanledexian.spring.cloud.producer.api.IHelloApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator
*/
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class HomeController {
@Autowired
private IHelloApi helloApi;
@GetMapping("/sayHello")
public String sayHello(String name) {
name += "!";
return helloApi.sayHello(name);
}
}
server:
# 服务器端口
port: 9202
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/
spring:
application:
name: service-consumer-feign
......@@ -8,8 +8,22 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-provider</artifactId>
<description>Service Provider</description>
<artifactId>hello-consumer-ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
</project>
\ No newline at end of file
package com.duanledexian.spring.cloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class SpringWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebApplication.class, args);
}
}
package com.duanledexian.spring.cloud.producer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author Administrator
*/
@Configuration
public class AppConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.duanledexian.spring.cloud.producer.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author Administrator
*/
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class HomeController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/sayHello")
public String sayHello(String name) {
name += "!";
String url = "http://service-producer/api/v1/sayHello?name=" + name;
return restTemplate.getForObject(url, String.class);
}
}
server:
# 服务器端口
port: 9201
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/
spring:
application:
name: service-consumer-ribbon
server:
# 服务器端口
port: 9201
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/
spring:
application:
name: service-consumer-ribbon
......@@ -10,5 +10,20 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
</project>
\ No newline at end of file
package com.duanledexian.spring.cloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class SpringWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebApplication.class, args);
}
}
package com.duanledexian.spring.cloud.producer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author Administrator
*/
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.duanledexian.spring.cloud.producer.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author Administrator
*/
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class HomeController {
@Autowired
private LoadBalancerClient client;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/sayHello")
public String sayHello(String name) {
name += "!";
ServiceInstance instance = client.choose("service-consumer");
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/v1/sayHello?name=" + name;
return restTemplate.getForObject(url, String.class);
}
}
server:
# 服务器端口
port: 9200
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/
spring:
application:
name: service-consumer
<?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">
<parent>
<artifactId>spring-cloud-hello-world</artifactId>
<groupId>com.duanledexian.spring.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-producer</artifactId>
<description>Service producer</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
package com.duanledexian.spring.cloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 服务提供方
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaClient
public class SpringWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebApplication.class, args);
}
}
package com.duanledexian.spring.cloud.producer.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author Administrator
*/
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class HomeController {
@Value("${server.port}")
String port;
@GetMapping("/sayHello")
public String sayHello(String name) {
return "hi " + name + " ,i am from port:" + port + " time:" + new Date();
}
}
server:
# 服务器端口
port: 9100
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/
spring:
application:
name: service-producer
......@@ -13,8 +13,10 @@
<packaging>pom</packaging>
<modules>
<module>service-discovery</module>
<module>hello-provider</module>
<module>hello-producer</module>
<module>hello-consumer</module>
<module>hello-consumer-ribbon</module>
<module>hello-consumer-feign</module>
</modules>
<dependencies>
......@@ -29,4 +31,4 @@
</dependency>
</dependencies>
</project>
\ No newline at end of file
</project>
package com.duanledexian.spring.cloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Eureka 服务注册中心
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
server:
# 服务器端口
port: 9000
eureka:
instance:
hostname: localhost
client:
# 在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
# 表示是否将自己注册到 Eureka Server,默认为 true。
register-with-eureka: false
# 表示是否从 Eureka Server 获取注册信息,默认为 true。
fetch-registry: false
# 设置与 Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址。默认是 http://localhost:9000/eureka ;多个地址可使用英文逗号(,)分隔。
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment