2018-08-10|2018-08-10 Eureka + MySQL to provide service

Step 1 Build the base Eureka-client project

  1. Build a project that acts as a Eureka-client
4.0.0 com.example eureka-provider 0.0.1-SNAPSHOTorg.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 1.8 【2018-08-10|2018-08-10 Eureka + MySQL to provide service】org.springframework.boot spring-boot-maven-plugin org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import

  1. Create the main-entry for the app
Content of src\main\java\app\EurekaClientApp.java
package app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class EurekaClientApp { public static void main(String[] args) { SpringApplication.run(EurekaClientApp.class, args); } }

  1. Prepare application.yml to configure the client
Content of application.yml
spring: application: name: eureka-clientserver: port: 8766eureka: client: register-with-eureka: true serviceUrl: defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/ instance: ip-address: 127.0.0.1 prefer-ip-address: true

Step 2 Add dependency to access MySQL through JPA
  1. Add the dependency in the pom.xml
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java

  1. Update the application.yml file, to define the parameters to MySQL
Content of application.yml
spring: application: name: eureka-client jpa: database: MYSQL show-sql: true hibernate: ddl-auto: none datasource: url: jdbc:mysql://127.0.0.1:3306/xiaoyouhui username: root password: password123 driver-class-name: com.mysql.jdbc.Driverserver: port: 8766eureka: client: register-with-eureka: true serviceUrl: defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/ instance: ip-address: 127.0.0.1 prefer-ip-address: true

  1. Create the model-class
package app.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "member") public class Member{ //fields //getters & setters }

  1. Create the repository
package app.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import domain.Member; @Repository public interface MemberRepository extends JpaRepository{}

Step 3 Add a controller to handle request The controller-class should under the package of the main-entry. Since Spring-boot will scan from the root-package.
Content of src\main\java\app\controller\MainController.java
package app.controller; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import domain.Member; import repository.MemberRepository; @RestController public class MainController { @Autowired private MemberRepository memberRepository; @RequestMapping("/greeting") public Optional greeting(@RequestParam(value="https://www.it610.com/article/id", defaultValue="https://www.it610.com/article/na") String id) { return memberRepository.findById(id); }@RequestMapping("/greetings") public List greetings() { List list = memberRepository.findAll(); System.out.println(list.size()); return list; }@GetMapping(path="/user/{id}") public String findById(@PathVariable Long id) { return "hello"; }@RequestMapping("/test") public @ResponseBody String testm() { return "test"; } }

Step 4 Configure to open some Actuator end-point
management: endpoints: web: exposure: include: "*" endpoint: shutdown: enabled: false

References
  • Spring document to access MySQL
  • Spring document to build a restful service
  • Blog post to configure Actuator

    推荐阅读